//
//  RecommendViewController.m
//  OurTourism
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "RecommendViewController.h"
#import "Header.h"
#import "MZRecommendModel.h"
#import "MZRecommendCollectionViewCell.h"
#import "MZCollectionReusableView.h"
#import "MZMoreViewController.h"
#import "MZRecommendPushViewController.h"

@interface RecommendViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *dataArr;
@property(nonatomic, strong) MBProgressHUD *HUB;
@property(nonatomic, assign) NSInteger *indexSection;

@end

@implementation RecommendViewController

//#pragma mark - 白色status bar
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    self.navigationItem.title = @"目的地";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.dataArr = [NSMutableArray array];
    
    [self createCollectionView];
    [self prepareData];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUB show:YES];
    self.HUB.labelText = @"正在加载";
}

#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    layout.minimumLineSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGTH - 69) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.collectionView registerClass:[MZRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"recommendItem"];
    [self.collectionView registerClass:[MZCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"recommendHeader"];
    [self.view addSubview:self.collectionView];
}

#pragma mark - 获取数据
- (void)prepareData
{
    NSString *url = @"http://api.breadtrip.com/destination/v3/";
    [MZNetHandle getUrl:url cookie:nil result:^(id result) {
       
        NSArray *arr = result[@"elements"];
        [self.dataArr addObjectsFromArray:[MZRecommendModel transformWithArray:arr]];
        
        [self.collectionView reloadData];
        [self.HUB removeFromSuperview];
    }];
}

#pragma mark - collectionView协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.dataArr[section] dataValue].count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MZRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommendItem" forIndexPath:indexPath];
    NSArray *arr = [self.dataArr[indexPath.section] dataValue];
    NSMutableArray *mArr = [MZRecommendModel transformWithArray:arr];
    cell.temp = mArr[indexPath.item];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    MZCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"recommendHeader" forIndexPath:indexPath];
    headerView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    headerView.tempModel = self.dataArr[indexPath.section];
    headerView.index = indexPath.section;
    [headerView.moreBtn addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    headerView.moreBtn.tag = indexPath.section;
    
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MZRecommendPushViewController *rpVC = [[MZRecommendPushViewController alloc] init];
//    rpVC.tempDic = [self.dataArr[indexPath.section] dataValue][indexPath.item];
    
    rpVC.tempModel = [MZRecommendModel transformWithArray:[self.dataArr[indexPath.section] dataValue]][indexPath.item];
    
    [self.navigationController pushViewController:rpVC animated:YES];
}

- (void)moreAction:(UIButton *)sender
{
    MZMoreViewController *moreVC = [[MZMoreViewController alloc] init];
    moreVC.tempModel = self.dataArr[sender.tag];
    [self.navigationController pushViewController:moreVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
